//
//  AiActViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/4/11.
//

import Foundation
class AiActViewModel:BaseViewModel {
    
    
    func doMonthFree() {
        if !userManager.isLogin(){
            showLoginPopup()
            return
        }
        if (userManager.isInfinite()) {
            showToast("当前是AI不限制，无法领取")
            return
        }
        request {
            let sign = try await TimeUtils.getDate(time:getNetTime(), format: "yyyy-MM")
            try check(sign != userManager.getExtraConfig().aiActMonthSign) {
                "当月已领取过了，请下个月再来"
            }
            let count = userManager.isVip() ? (configManager.getConfig().aiChat.aiMonthVipCount ?? 0 ) : (configManager.getConfig().aiChat.aiMonthCount ?? 0)
            try check(count != 0) {
                "活动已结束"
            }
            userManager.getExtraConfig().aiActMonthSign = sign
            try await userManager.requestAsync(user: userManager.user)
            return count
        } suc: {
            userManager.doAiChatCount(Double($0))
            showTipsDialog(text: "已成功领取\($0)次AI次数") {}
        } fail: {
            showToast($0.message)
        }
    }
    
    func doDayFree() {
        if !userManager.isLogin(){
            showLoginPopup()
            return
        }
        if (userManager.isInfinite()) {
            showToast("当前是AI不限制，无法领取")
            return
        }
        request {
            let sign = try await TimeUtils.getDate(time:getNetTime(), format: "yyyy-MM-dd")
            try check(sign != userManager.getExtraConfig().aiActDaySign) {
                "当天已领取过了，请明天再来"
            }
            let count = userManager.isVip() ? (configManager.getConfig().aiChat.aiDayVipCount ?? 0 ) : (configManager.getConfig().aiChat.aiDayCount ?? 0)
            try check(count != 0) {
                "活动已结束"
            }
            userManager.getExtraConfig().aiActDaySign = sign
            try await userManager.requestAsync(user: userManager.user)
            return count
        } suc: {
            userManager.doAiChatCount(Double($0))
            showTipsDialog(text: "已成功领取\($0)次AI次数") {}
        } fail: {
            showToast($0.message)
        }
    }
}
